package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.constant.TableNameConstant;
import cc.rengu.igas.bomp.common.dao.*;
import cc.rengu.igas.bomp.common.dao.impl.*;
import cc.rengu.igas.bomp.common.entity.*;
import cc.rengu.igas.bomp.common.enums.*;
import cc.rengu.igas.bomp.common.util.InitParamUtil;
import cc.rengu.igas.bomp.core.model.UserSessionInfo;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.CashWithdrawalFeeInfoBean;
import cc.rengu.igas.bomp.facade.bean.SettleAcctInfoBean;
import cc.rengu.igas.bomp.facade.bean.SignImageInfoBean;
import cc.rengu.igas.bomp.facade.request.MchntSettleAcctInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.MchntSettleAcctInfoQueryResponse;
import cc.rengu.igas.share.common.util.SensitiveDataUtil;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.*;
import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.CollectionUtils;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 商户结算信息查询
 * Created by 赵奎伍
 */
public class MchntSettleInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntSettleAcctInfoQueryRequest mchntSettleAcctInfoQueryRequest = new MchntSettleAcctInfoQueryRequest();
        ConvertUtil.convertOutput(mchntSettleAcctInfoQueryRequest);
        return mchntSettleAcctInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        MchntSettleAcctInfoQueryRequest mchntSettleAcctInfoQueryRequest = (MchntSettleAcctInfoQueryRequest) request;
        MchntSettleAcctInfoQueryResponse mchntSettleAcctInfoQueryResponse = new MchntSettleAcctInfoQueryResponse();
        BizResponse<MchntSettleAcctInfoQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setResult(mchntSettleAcctInfoQueryResponse);
        /*用户标识*/
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String userRole = xmlTreeUtil.getXmlTreeStringValue("userRole");

        MchntBaseInfo mchntBaseInfo;
        List<SettleAcctInfoBean> settleAcctInfoBeans = new ArrayList<>();
        String bizType = mchntSettleAcctInfoQueryRequest.getHeader().getBizType();
        if (StringUtil.isEmptyOrNull(bizType)) {
            rglog.error("bizType<{}>为NULL!,无法查询信息", bizType);
            bizResponse.setRespCode(RespCodeEnum.PARAM_ERROR.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.PARAM_ERROR.getRespDesc());
            bizResponse.setResult(mchntSettleAcctInfoQueryResponse);
            return bizResponse;
        }

        if (QueryBizTypeEnum.QUERY_TEMPORARY_TABLE.getType().equals(bizType)) {
            /* 查询临时表数据 */
            BizTmpMapper bizTmpMapper = new BizTmpMapperImpl();
            List<BizTmp> bizAcctTempList = bizTmpMapper.selectBizTmpByMchntNo(mchntSettleAcctInfoQueryRequest.getHeader().getInstId(), mchntSettleAcctInfoQueryRequest.getMchntNo(), TableNameConstant.T_B_MCHNT_ACCT_INFO);
            if (!CollectionUtils.isEmpty(bizAcctTempList)) {
                for (BizTmp bizAcctTmp : bizAcctTempList) {
                    String mchntAcctInfoString = InitParamUtil.getJsonString(bizAcctTmp);
                    MchntAcctInfo mchntAcctInfo = JSON.parseObject(mchntAcctInfoString, MchntAcctInfo.class);

                    if (AppParamConstant.YES.equals(mchntAcctInfo.getRecordStatus())) {
                        convSettleAcctInfo(mchntAcctInfo, settleAcctInfoBeans, userRole, mchntSettleAcctInfoQueryRequest);
                    }
                    mchntSettleAcctInfoQueryResponse.setSettleAcctInfoList(settleAcctInfoBeans);
                }
                List<BizTmp> bizTmpList = bizTmpMapper.selectBizTmpByMchntNo(mchntSettleAcctInfoQueryRequest.getHeader().getInstId(), mchntSettleAcctInfoQueryRequest.getMchntNo(), TableNameConstant.T_B_MCHNT_BASE_INFO);
                if (null != bizTmpList && !bizTmpList.isEmpty()) {
                    BizTmp bizTmp = bizTmpList.get(0);
                    String mchntBaseInfoString = InitParamUtil.getJsonString(bizTmp);
                    mchntBaseInfo = JSON.parseObject(mchntBaseInfoString, MchntBaseInfo.class);
                    mchntSettleAcctInfoQueryResponse.setRealSettleFlag(mchntBaseInfo.getRealSettleFlag());
                    mchntSettleAcctInfoQueryResponse.setMchntSettleMode(mchntBaseInfo.getMchntSettleMode());
                    mchntSettleAcctInfoQueryResponse.setCashWithdrawalFlag(mchntBaseInfo.getCashWithdrawalFlag());

                    mchntSettleAcctInfoQueryResponse.setAuditStatus(mchntBaseInfo.getAuditStatus());
                    mchntSettleAcctInfoQueryResponse.setAuditRefuseReason(mchntBaseInfo.getAuditRefuseReason());
                    mchntSettleAcctInfoQueryResponse.setLastOprId(mchntBaseInfo.getLastOprId());
                    mchntSettleAcctInfoQueryResponse.setLastUpdateTime(mchntBaseInfo.getLastUpdateTime());
                }

                List<BizTmp> bizFeeTempList = bizTmpMapper.selectBizTmpByMchntNo(mchntSettleAcctInfoQueryRequest.getHeader().getInstId(), mchntSettleAcctInfoQueryRequest.getMchntNo(), TableNameConstant.T_B_MCHNT_FEE_INFO);
                if (!CollectionUtils.isEmpty(bizFeeTempList)) {
                    List<MchntFeeInfo> d0FeeList = bizFeeTempList.stream().map(item -> JSON.parseObject(InitParamUtil.getJsonString(item), MchntFeeInfo.class)).collect(Collectors.toList());
                    d0FeeList = d0FeeList.stream().filter(item -> MchntFeeTypeEnum.DO.getType().equals(item.getFeeType()) && AppParamConstant.YES.equals(item.getRecordStatus())).collect(Collectors.toList());

                    if (!CollectionUtils.isEmpty(d0FeeList)) {
                        //按生效日期倒叙排列
                        d0FeeList = d0FeeList.stream().sorted(Comparator.comparing(MchntFeeInfo::getFeeEffectiveDate)).collect(Collectors.toList());
                        Collections.reverse(d0FeeList);
                        CashWithdrawalFeeInfoBean cashWithdrawalFeeInfoBean = new CashWithdrawalFeeInfoBean();
                        BeanUtil.beanCopy(d0FeeList.get(0), cashWithdrawalFeeInfoBean);
                    }
                }
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

            }
        } else {
            mchntBaseInfo = getMchntAcctInfoByFormalTable(mchntSettleAcctInfoQueryRequest, mchntSettleAcctInfoQueryResponse, bizResponse, userRole, settleAcctInfoBeans);
            /* 查询结果赋值 */
            mchntSettleAcctInfoQueryResponse.setSettleAcctInfoList(settleAcctInfoBeans);
            mchntSettleAcctInfoQueryResponse.setAuditStatus(mchntBaseInfo.getAuditStatus());
            mchntSettleAcctInfoQueryResponse.setAuditRefuseReason(mchntBaseInfo.getAuditRefuseReason());
            mchntSettleAcctInfoQueryResponse.setLastOprId(mchntBaseInfo.getLastOprId());
            mchntSettleAcctInfoQueryResponse.setLastUpdateTime(mchntBaseInfo.getLastUpdateTime());
        }
        return bizResponse;
    }

    private MchntBaseInfo getMchntAcctInfoByFormalTable(MchntSettleAcctInfoQueryRequest mchntSettleAcctInfoQueryRequest, MchntSettleAcctInfoQueryResponse mchntSettleAcctInfoQueryResponse, BizResponse<MchntSettleAcctInfoQueryResponse> bizResponse, String userRole, List<SettleAcctInfoBean> settleAcctInfoBeans) throws Exception {
        MchntBaseInfo mchntBaseInfo;/*查询商户基本信息表 */
        MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
        mchntBaseInfo = mchntBaseInfoMapper.selectMchntBaseInfoByPrimaryKey(mchntSettleAcctInfoQueryRequest.getHeader().getInstId(), mchntSettleAcctInfoQueryRequest.getMchntNo());
        if (null == mchntBaseInfo) {
            rglog.error("商户信息为空，商户号<{}>!", mchntSettleAcctInfoQueryRequest.getMchntNo());
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        mchntSettleAcctInfoQueryResponse.setRealSettleFlag(mchntBaseInfo.getRealSettleFlag());
        mchntSettleAcctInfoQueryResponse.setMchntSettleMode(mchntBaseInfo.getMchntSettleMode());
        mchntSettleAcctInfoQueryResponse.setCashWithdrawalFlag(mchntBaseInfo.getCashWithdrawalFlag());

        /* 查询商户结算信息表 */
        MchntAcctInfoMapper mchntAcctInfoMapper = new MchntAcctInfoMapperImpl();
        List<MchntAcctInfo> mchntAcctInfoList = mchntAcctInfoMapper.selectMchntAcctInfoByMchntNo(mchntSettleAcctInfoQueryRequest.getHeader().getInstId(), mchntSettleAcctInfoQueryRequest.getMchntNo());
        if (null != mchntAcctInfoList && !mchntAcctInfoList.isEmpty()) {
            /* 按照创建时间降序排序 */
            List<MchntAcctInfo> acctInfoList = mchntAcctInfoList.stream().sorted(Comparator.comparing(MchntAcctInfo::getCreateTime)).collect(Collectors.toList());
            Collections.reverse(acctInfoList);
            acctInfoList.forEach(item -> convSettleAcctInfo(item, settleAcctInfoBeans, userRole, mchntSettleAcctInfoQueryRequest));
            mchntSettleAcctInfoQueryResponse.setSettleAcctInfoList(settleAcctInfoBeans);
            MchntFeeInfoMapper mchntFeeInfoMapper = new MchntFeeInfoMapperImpl();
            MchntFeeInfo mchntFeeInfo = mchntFeeInfoMapper.selectMchntFeeInfoByCondition(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo(), "*", MchntFeeTypeEnum.DO.getType(), DateUtil.getCurrentDate());
            if (null != mchntFeeInfo) {
                CashWithdrawalFeeInfoBean cashWithdrawalFeeInfoBean = new CashWithdrawalFeeInfoBean();
                BeanUtil.beanCopy(mchntFeeInfo, cashWithdrawalFeeInfoBean);
            }
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        } else {
            rglog.error("法人行<{}>无商户结算信息!", mchntSettleAcctInfoQueryRequest.getHeader().getInstId());
            bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
        }
        return mchntBaseInfo;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntSettleAcctInfoQueryResponse mchntSettleAcctInfoQueryResponse = (MchntSettleAcctInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntSettleAcctInfoQueryResponse);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param mchntAcctInfo       数据库表结构对象
     * @param settleAcctInfoBeans 接口对象
     */
    private void convSettleAcctInfo(MchntAcctInfo mchntAcctInfo, List<SettleAcctInfoBean> settleAcctInfoBeans, String userRole, MchntSettleAcctInfoQueryRequest mchntSettleAcctInfoQueryRequest) {
        try {
            if (!AppParamConstant.YES.equals(mchntAcctInfo.getRecordStatus())) {
                return;
            }
            String bizType = mchntSettleAcctInfoQueryRequest.getHeader().getBizType();
            SettleAcctInfoBean settleAcctInfoBean = new SettleAcctInfoBean();
            BeanUtil.beanCopy(mchntAcctInfo, settleAcctInfoBean);
            //结算账户账号 赋值
            if (!StringUtil.isEmptyOrNull(userRole) && BompAppParamConstant.MCHNT_ROLES.contains(userRole)) {
                String sessionId = mchntSettleAcctInfoQueryRequest.getHeader().getSession();
                String sessionObjStr = RedisUtil.onceGet(sessionId);
                UserSessionInfo userSessionInfo = JSON.parseObject(sessionObjStr, UserSessionInfo.class);
                if (null == userSessionInfo) {
                    rglog.error("session过期");
                    throw new BizException(RespCodeEnum.USER_NOT_LOGIN_OR_SESSION_IS_EXPIRED.getRespCode(), RespCodeEnum.USER_NOT_LOGIN_OR_SESSION_IS_EXPIRED.getRespDesc());
                }
                Map<String, String> sensitiveMap = userSessionInfo.getSensitiveMap();
                if (CollectionUtils.isEmpty(sensitiveMap)) {
                    sensitiveMap = new HashMap<>();
                }
                String acctNo = SensitiveDataUtil.desensitization("2", mchntAcctInfo.getSettleAcctNo());
                rglog.info("acctNo:" + acctNo);
                sensitiveMap.put(acctNo, mchntAcctInfo.getSettleAcctNo());
                userSessionInfo.setSensitiveMap(sensitiveMap);
                RedisUtil.onceSet(sessionId, JSON.toJSONString(userSessionInfo), BompAppParamConstant.SESSION_EFFECTIVE_TIME);
                settleAcctInfoBean.setSettleAcctNo(acctNo);
            } else {
                settleAcctInfoBean.setSettleAcctNo(mchntAcctInfo.getSettleAcctNo());
            }
            settleAcctInfoBean.setSettleAcctBankNo(mchntAcctInfo.getAcctOpenBankNo());
            settleAcctInfoBean.setSettleAcctBankName(mchntAcctInfo.getAcctOpenBankName());
            //结算账户账号 赋值
            settleAcctInfoBean.setSettleAcctName(mchntAcctInfo.getSettleAcctName());
            //结算账户开户行名称 赋值
            settleAcctInfoBean.setSettleAcctBankName(mchntAcctInfo.getAcctOpenBankName());
            //结算账户开户地址-省 赋值
            settleAcctInfoBean.setSettleAcctBankProvince(mchntAcctInfo.getAcctOpenProvince());
            //结算账户开户行地址-省名称 赋值
            settleAcctInfoBean.setSettleAcctBankProvinceName(mchntAcctInfo.getAcctOpenProvince());
            //结算账户开户行地址-市 赋值
            settleAcctInfoBean.setSettleAcctBankCity(mchntAcctInfo.getAcctOpenCity());
            //结算账户开户行地址-市名称 赋值
            settleAcctInfoBean.setSettleAcctBankCityName(mchntAcctInfo.getAcctOpenCity());
            //结算账户开户行地址-区 赋值
            settleAcctInfoBean.setSettleAcctBankArea(mchntAcctInfo.getAcctOpenArea());
            //结算账户开户行地址-区名称 赋值
            settleAcctInfoBean.setSettleAcctBankAreaName(mchntAcctInfo.getAcctOpenArea());
            /* 获取行政区域位置信息 */
            RegionInfoMapper regionInfoMapper = new RegionInfoMapperImpl();
            RegionInfo regionInfo = null;
            if (!StringUtil.isEmptyOrNull(mchntAcctInfo.getAcctOpenProvince())) {
                regionInfo = regionInfoMapper.selectRegionInfoByPrimaryKey(mchntAcctInfo.getAcctOpenProvince());
                if (null != regionInfo) {
                    settleAcctInfoBean.setSettleAcctBankProvinceName(regionInfo.getRegionName());
                }
            }
            if (!StringUtil.isEmptyOrNull(mchntAcctInfo.getAcctOpenCity())) {
                regionInfo = regionInfoMapper.selectRegionInfoByPrimaryKey(mchntAcctInfo.getAcctOpenCity());
                if (null != regionInfo) {
                    settleAcctInfoBean.setSettleAcctBankCityName(regionInfo.getRegionName());
                }
            }
            if (!StringUtil.isEmptyOrNull(mchntAcctInfo.getAcctOpenArea())) {
                regionInfo = regionInfoMapper.selectRegionInfoByPrimaryKey(mchntAcctInfo.getAcctOpenArea());
                if (null != regionInfo) {
                    settleAcctInfoBean.setSettleAcctBankAreaName(regionInfo.getRegionName());
                }
            }

            String imageType = FundTypeEnum.SETTLE_TYPE.getFundCode().equals(mchntAcctInfo.getFundType()) ? ImageTypeEnum.NET_SETTLEMENT_ACCT.getImageType() : ImageTypeEnum.FEE_SETTLEMENT_ACCT.getImageType();
            //结算账户影像存储路径 赋值
            if (QueryBizTypeEnum.QUERY_FORMAL_TABLE.getType().equals(bizType)) {
                MchntImageInfoMapper mchntImageInfoMapper = new MchntImageInfoMapperImpl();
                List<MchntImageInfo> mchntImageInfoList = mchntImageInfoMapper.selectMchntImageInfoByMchntNoAndImageType(mchntAcctInfo.getInstId(), mchntAcctInfo.getMchntNo(), imageType);
                if (!CollectionUtils.isEmpty(mchntImageInfoList)) {
                    SignImageInfoBean signImageInfoBean = new SignImageInfoBean();
                    List<MchntImageInfo> imageInfoList = mchntImageInfoList.stream().filter(item -> AppParamConstant.YES.equals(item.getRecordStatus())).sorted(Comparator.comparing(MchntImageInfo::getCreateTime)).collect(Collectors.toList());
                    Collections.reverse(imageInfoList);
                    if (!CollectionUtils.isEmpty(imageInfoList)) {
                        List<SignImageInfoBean> settleImageList = new ArrayList<>();
                        BeanUtil.beanCopy(imageInfoList.get(0), signImageInfoBean);
                        signImageInfoBean.setId(imageInfoList.get(0).getId());
                        signImageInfoBean.setImageIndex(Integer.toString(imageInfoList.get(0).getImageIndex()));
                        signImageInfoBean.setImageId(imageInfoList.get(0).getImageId());
                        signImageInfoBean.setAlipayImageId(imageInfoList.get(0).getAlipayImageId());
                        signImageInfoBean.setWechatImageId(imageInfoList.get(0).getWechatMediaId());
                        if (!StringUtil.isEmptyOrNull(imageInfoList.get(0).getImageRemotePath())) {
                            signImageInfoBean.setImagePath(Base64.encodeBase64String(imageInfoList.get(0).getImageRemotePath().getBytes()));
                        }
                        signImageInfoBean.setImageLocalPath(Base64.encodeBase64String(imageInfoList.get(0).getImageLocalPath().getBytes()));
                        signImageInfoBean.setImageDesc(imageInfoList.get(0).getImageDesc());
                        settleImageList.add(signImageInfoBean);

                        //判断是否有资金管理关系证明照
                        List<MchntImageInfo> fundImageInfoList = mchntImageInfoMapper.selectMchntImageInfoByMchntNoAndImageType(mchntAcctInfo.getInstId(), mchntAcctInfo.getMchntNo(), ImageTypeEnum.FUND_MANAGE.getImageType());
                        if (!CollectionUtils.isEmpty(fundImageInfoList)) {
                            //根据结算账户获取这张照片的imageIndex  结算账户取值为0 手续费账户取值为1
                            int index = FundTypeEnum.SETTLE_TYPE.getFundCode().equals(mchntAcctInfo.getFundType()) ? 0 : 1;
                            List<MchntImageInfo> fundImageList = fundImageInfoList.stream().filter(item -> AppParamConstant.YES.equals(item.getRecordStatus()) && (item.getImageIndex() == index)).collect(Collectors.toList());
                            if (!CollectionUtils.isEmpty(fundImageList)) {
                                SignImageInfoBean fundImageInfo = new SignImageInfoBean();
                                BeanUtil.beanCopy(fundImageList.get(0), fundImageInfo);
                                fundImageInfo.setId(fundImageList.get(0).getId());
                                fundImageInfo.setImageIndex(Integer.toString(fundImageList.get(0).getImageIndex()));
                                fundImageInfo.setImageId(fundImageList.get(0).getImageId());
                                fundImageInfo.setAlipayImageId(fundImageList.get(0).getAlipayImageId());
                                fundImageInfo.setWechatImageId(fundImageList.get(0).getWechatMediaId());
                                if (!StringUtil.isEmptyOrNull(fundImageList.get(0).getImageRemotePath())) {
                                    fundImageInfo.setImagePath(Base64.encodeBase64String(fundImageList.get(0).getImageRemotePath().getBytes()));
                                }
                                fundImageInfo.setImageLocalPath(Base64.encodeBase64String(fundImageList.get(0).getImageLocalPath().getBytes()));
                                fundImageInfo.setImageDesc(fundImageList.get(0).getImageDesc());
                                settleImageList.add(fundImageInfo);
                            }
                        }

                        settleAcctInfoBean.setSettleImageInfoList(settleImageList);
                    }
                }
            } else {
                BizTmpMapper bizTmpMapper = new BizTmpMapperImpl();
                List<BizTmp> bizTmpList = bizTmpMapper.selectBizTmpByMchntNo(mchntAcctInfo.getInstId(), mchntAcctInfo.getMchntNo(), TableNameConstant.T_B_MCHNT_IMAGE_INFO);
                if (!CollectionUtils.isEmpty(bizTmpList)) {

                    List<MchntImageInfo> bizImageList = bizTmpList.stream().map(item -> JSON.parseObject(InitParamUtil.getJsonString(item), MchntImageInfo.class)).collect(Collectors.toList());
                    List<MchntImageInfo> acctImageList = bizImageList.stream().filter(item -> AppParamConstant.YES.equals(item.getRecordStatus()) && imageType.equals(item.getImageType())).sorted(Comparator.comparing(MchntImageInfo::getCreateTime)).collect(Collectors.toList());

                    if (!CollectionUtils.isEmpty(acctImageList)) {
                        List<SignImageInfoBean> settleImageList = new ArrayList<>();
                        SignImageInfoBean signImageInfoBean = new SignImageInfoBean();
                        BeanUtil.beanCopy(acctImageList.get(0), signImageInfoBean);
                        signImageInfoBean.setId(acctImageList.get(0).getId());
                        signImageInfoBean.setImageIndex(Integer.toString(acctImageList.get(0).getImageIndex()));
                        signImageInfoBean.setImageId(acctImageList.get(0).getImageId());
                        signImageInfoBean.setAlipayImageId(acctImageList.get(0).getAlipayImageId());
                        signImageInfoBean.setWechatImageId(acctImageList.get(0).getWechatMediaId());
                        if (!StringUtil.isEmptyOrNull(acctImageList.get(0).getImageRemotePath())) {
                            signImageInfoBean.setImagePath(Base64.encodeBase64String(acctImageList.get(0).getImageRemotePath().getBytes()));
                        }
                        signImageInfoBean.setImageLocalPath(Base64.encodeBase64String(acctImageList.get(0).getImageLocalPath().getBytes()));
                        signImageInfoBean.setImageDesc(acctImageList.get(0).getImageDesc());
                        settleImageList.add(signImageInfoBean);

                        List<MchntImageInfo> fundImageList = bizImageList.stream().filter(item -> AppParamConstant.YES.equals(item.getRecordStatus()) && ImageTypeEnum.FUND_MANAGE.getImageType().equals(item.getImageType())).collect(Collectors.toList());
                        if (!CollectionUtils.isEmpty(fundImageList)) {
                            //根据结算账户获取这张照片的imageIndex  结算账户取值为0 手续费账户取值为1
                            int index = FundTypeEnum.SETTLE_TYPE.getFundCode().equals(mchntAcctInfo.getFundType()) ? 0 : 1;
                            fundImageList = fundImageList.stream().filter(item -> AppParamConstant.YES.equals(item.getRecordStatus()) && (item.getImageIndex() == index)).collect(Collectors.toList());
                            if (!CollectionUtils.isEmpty(fundImageList)) {
                                SignImageInfoBean fundImageInfo = new SignImageInfoBean();
                                BeanUtil.beanCopy(fundImageList.get(0), fundImageInfo);
                                fundImageInfo.setId(fundImageList.get(0).getId());
                                fundImageInfo.setImageIndex(Integer.toString(fundImageList.get(0).getImageIndex()));
                                fundImageInfo.setImageId(fundImageList.get(0).getImageId());
                                fundImageInfo.setAlipayImageId(fundImageList.get(0).getAlipayImageId());
                                fundImageInfo.setWechatImageId(fundImageList.get(0).getWechatMediaId());
                                if (!StringUtil.isEmptyOrNull(fundImageList.get(0).getImageRemotePath())) {
                                    fundImageInfo.setImagePath(Base64.encodeBase64String(fundImageList.get(0).getImageRemotePath().getBytes()));
                                }
                                fundImageInfo.setImageLocalPath(Base64.encodeBase64String(fundImageList.get(0).getImageLocalPath().getBytes()));
                                fundImageInfo.setImageDesc(fundImageList.get(0).getImageDesc());
                                settleImageList.add(fundImageInfo);
                            }

                        }

                        settleAcctInfoBean.setSettleImageInfoList(settleImageList);
                    } else {
                        MchntImageInfoMapper mchntImageInfoMapper = new MchntImageInfoMapperImpl();
                        List<MchntImageInfo> mchntImageInfoList = mchntImageInfoMapper.selectMchntImageInfoByMchntNoAndImageType(mchntAcctInfo.getInstId(), mchntAcctInfo.getMchntNo(), imageType);
                        if (!CollectionUtils.isEmpty(mchntImageInfoList)) {
                            List<SignImageInfoBean> settleImageList = new ArrayList<>();
                            SignImageInfoBean signImageInfoBean = new SignImageInfoBean();
                            List<MchntImageInfo> imageInfoList = mchntImageInfoList.stream().filter(item -> AppParamConstant.YES.equals(item.getRecordStatus())).sorted(Comparator.comparing(MchntImageInfo::getCreateTime)).collect(Collectors.toList());
                            Collections.reverse(imageInfoList);
                            if (!CollectionUtils.isEmpty(imageInfoList)) {
                                BeanUtil.beanCopy(imageInfoList.get(0), signImageInfoBean);
                                signImageInfoBean.setId(imageInfoList.get(0).getId());
                                signImageInfoBean.setImageIndex(Integer.toString(imageInfoList.get(0).getImageIndex()));
                                signImageInfoBean.setImageId(imageInfoList.get(0).getImageId());
                                signImageInfoBean.setAlipayImageId(imageInfoList.get(0).getAlipayImageId());
                                signImageInfoBean.setWechatImageId(imageInfoList.get(0).getWechatMediaId());
                                if (!StringUtil.isEmptyOrNull(imageInfoList.get(0).getImageRemotePath())) {
                                    signImageInfoBean.setImagePath(Base64.encodeBase64String(imageInfoList.get(0).getImageRemotePath().getBytes()));
                                }
                                signImageInfoBean.setImageLocalPath(Base64.encodeBase64String(imageInfoList.get(0).getImageLocalPath().getBytes()));
                                signImageInfoBean.setImageDesc(imageInfoList.get(0).getImageDesc());


                                //判断是否有资金管理关系证明照
                                List<MchntImageInfo> fundImageInfoList = mchntImageInfoMapper.selectMchntImageInfoByMchntNoAndImageType(mchntAcctInfo.getInstId(), mchntAcctInfo.getMchntNo(), ImageTypeEnum.FUND_MANAGE.getImageType());
                                if (!CollectionUtils.isEmpty(fundImageInfoList)) {
                                    //根据结算账户获取这张照片的imageIndex  结算账户取值为0 手续费账户取值为1
                                    int index = FundTypeEnum.SETTLE_TYPE.getFundCode().equals(mchntAcctInfo.getFundType()) ? 0 : 1;
                                    List<MchntImageInfo> fundImageList = mchntImageInfoList.stream().filter(item -> AppParamConstant.YES.equals(item.getRecordStatus()) && (item.getImageIndex() == index)).collect(Collectors.toList());
                                    if (!CollectionUtils.isEmpty(fundImageList)) {
                                        SignImageInfoBean fundImageInfo = new SignImageInfoBean();
                                        BeanUtil.beanCopy(fundImageList.get(0), fundImageInfo);
                                        fundImageInfo.setId(fundImageList.get(0).getId());
                                        fundImageInfo.setImageIndex(Integer.toString(fundImageList.get(0).getImageIndex()));
                                        fundImageInfo.setImageId(fundImageList.get(0).getImageId());
                                        fundImageInfo.setAlipayImageId(fundImageList.get(0).getAlipayImageId());
                                        fundImageInfo.setWechatImageId(fundImageList.get(0).getWechatMediaId());
                                        if (!StringUtil.isEmptyOrNull(fundImageList.get(0).getImageRemotePath())) {
                                            fundImageInfo.setImagePath(Base64.encodeBase64String(fundImageList.get(0).getImageRemotePath().getBytes()));
                                        }
                                        fundImageInfo.setImageLocalPath(Base64.encodeBase64String(fundImageList.get(0).getImageLocalPath().getBytes()));
                                        fundImageInfo.setImageDesc(fundImageList.get(0).getImageDesc());
                                        settleImageList.add(fundImageInfo);
                                    }
                                }

                                settleImageList.add(signImageInfoBean);
                                settleAcctInfoBean.setSettleImageInfoList(settleImageList);
                            }
                        }
                    }
                }
            }


            settleAcctInfoBeans.add(settleAcctInfoBean);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户结算信息查询详细信息转换失败:{}", byteArrayOutputStream.toString());
        }
    }
}
